﻿namespace LaserMark.Print.Service
{
    public class JJZPrint : IPrint
    {
        //定义一个对象管理器ID的变量.
        private E3_ID m_idEM = E3_ID.INVALID;

        //定义一个当前管理器内图层数量的数组变量.
        private E3_ID[]? m_idLayerList;

        //定义一个当前操作用图层ID的变量.
        private E3_ID m_idCurLayer = E3_ID.INVALID;

        //定义一个控制卡ID的变量.
        private E3_ID m_idMarker = E3_ID.INVALID;

        //定义一个对象元素的变量.
        private E3_ID m_EntID = E3_ID.INVALID;

        //定义一个对象列表变量,用于存放当前操作图层内的所有对象ID.
        private E3_ID[]? m_EntList;

        //定义一个当前操作图层的索引变量.
        private int m_nCurrentLayerIndex = 0;

        public Task<string> NetWorkInitAsync(bool isLinkByNet, byte[] ip)
        {
            var str = "";
            //设置一个bool变量,来判断是否开启网口卡的初始化,这个变量可以从界面控件获得值.
            //如果手里只有网口卡,也可以不使用这个变量,直接进行网口卡初始化接口的调用,请根据情况而定.
            if (isLinkByNet)
            {
                uint ipAddr = default;
                EzdKernel.E3_EthConvertStringToIP(ip, ref ipAddr);
                //调用网口卡初始化接口,这个接口要传入当前开发库的根目录,一般的情况为 运行程序根目录.
                EzdKernel.E3_EthRegisterCardByIP(ipAddr);
            }
            else //如果手里有USB和网口卡两种卡版本,那么这里如果不选择网口卡初始化,需要将卡类型写入参数配置文件.
            {
                try
                {
                    //定义变量,值为参数配置文件的路径.
                    string Controller = AppContext.BaseDirectory + "\\PARAM\\Controller.ini";
                    //定义变量,值为卡类型为USB的参数项.
                    string UsbLink = "[PARAM]\nTYPE=USB";
                    //调用文件写入方法,将参数写入到文件.
                    File.WriteAllText(Controller, UsbLink);
                }
                catch
                {
                    //如果出现写入异常,则弹框.
                    str = "修改参数错误!";
                }
            }

            return Task.FromResult(str);
        }

        public Task<string> CreateEntMgrAsync()
        {
            var str = "";
            EzdKernel.E3_DisableInitialPrompt();
            EzdKernel.E3_SetLanguageFile(AppContext.BaseDirectory + "\\LANG\\lang_Chs.ini");
            E3_ERR err = EzdKernel.E3_Initial(AppContext.BaseDirectory, 0);
            if (err == E3_ERR.ERR_EZCADRUN)
            {
                str = "请关闭当前正在运行的 EzCard 实例";
            }
            else
            {
                E3_ID m_idCurLayer = E3_ID.INVALID;
                m_idEM = EzdKernel.E3_CreateEntMgr(0);
                EzdKernel.E3_GetLayerId(m_idEM, 0, ref m_idCurLayer);
                m_idMarker = EzdKernel.E3_MarkerGetFirstValidId();
                str = "初始化完成";
            }

            return Task.FromResult(str);
        }

        public Task<string> Dispose()
        {
            if (m_idMarker != E3_ID.INVALID)
            {
                //如果控制卡ID正确获取的,那么调用控制卡释放接口,即会将控制卡释放掉.
                E3_ERR err = EzdKernel.E3_CloseMarker(m_idMarker);
            }

            //判断管理器ID是否不为0,也即在前面初始化时正确的创建.
            if (m_idEM != E3_ID.INVALID)
            {
                //如果管理器ID正确创建的,那么调用管理器释放接口,即会将管理器释放掉.
                E3_ERR err = EzdKernel.E3_FreeEntMgr(m_idEM);
            }

            //最后调用开发库释放接口即可,如若想重新初始化,请调用初始化接口.
            EzdKernel.E3_Close();

            return Task.FromResult("释放资源完成");
        }

        public Task<string> MarkerSetRotateMoveParam(double dRotateAng)
        {
            E3_ERR err, err2;
            err2 = E3_ERR.ERR_SUCCESS;
            err = EzdKernel.E3_MarkerSetRotateMoveParam(m_idMarker, 0, 0, 0, 0, dRotateAng);

            if (err == err2)
            {
                return Task.FromResult(err.ToString());
            }
            else
            {
                return Task.FromResult("");
            }
        }

        public Task<bool> MarkerCheckLaserState()
        {
            bool bIsOK = false;
            EzdKernel.E3_MarkerCheckLaserState(m_idMarker, ref bIsOK);
            return Task.FromResult(bIsOK);
        }

        public Task<string> LoadTemplateAsync(string filePath, out int m_nEntCount, out E3_ID[] m_EntList)
        {
            var str = "";
            //如果选择文件完成,那么就调用加载模板文件的接口,将文件名,之前创建的对象管理器ID,和两个默认为False的参数传给接口即可.
            E3_ERR err = EzdKernel.E3_OpenFileToEntMgr(filePath, m_idEM, false, false);
            //加载完成弹框显示接口的执行结果.
            str = err.ToString();
            //定义一个图层数量变量.
            int nLayerCount = 0;
            //通过查询图层数量的接口,查询查出模板中共有多少个图层.
            E3_ERR err1 = EzdKernel.E3_GetLayerCount(m_idEM, ref nLayerCount, ref m_nCurrentLayerIndex);
            //初始化这个图层数组.
            m_idLayerList = new E3_ID[nLayerCount];
            //循环查询得到的图层总数,来获取每个索引对应的图层ID,并保存到图层数组中.
            for (int i = 0; i < nLayerCount; i++)
            {
                m_idLayerList[i] = E3_ID.INVALID;
                E3_ERR err2 = EzdKernel.E3_GetLayerId(m_idEM, i, ref m_idLayerList[i]);
            }

            //设置当前操作的图层ID,通过当前操作的索引变量.
            m_idCurLayer = m_idLayerList[m_nCurrentLayerIndex];

            //
            m_nEntCount = 0;
            //得到指定图层指定对象数量
            EzdKernel.E3_GetEntCountInLayer(m_idCurLayer, ref m_nEntCount);
            //初始化对象列表数组,元素总数为当前图层得到的对象总数.
            m_EntList = new E3_ID[m_nEntCount];

            for (int i = 0; i < m_nEntCount; i++)
            {
                //循环对象索引,得到指定图层指定索引的对象ID.
                EzdKernel.E3_FindEntInLayerByIndex(m_idCurLayer, i, ref m_EntList[i]);
            }

            return Task.FromResult(str);
        }

        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<string> DrawImageAsync(IntPtr hdc, int nParam, int bmpwidth, int bmpheight,
            double dLogOriginX, double dLogOriginY, double dLogScale)
        {
            var aa = EzdKernel.E3_DrawEnt2(hdc, m_idCurLayer, 0, bmpwidth, bmpheight, dLogOriginX, dLogOriginY, dLogScale);

            return Task.FromResult("绘图完成");
        }

        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<int> MarkerMarkEnt2Async(E3_ID e3_ID)
        {
            int ret = 0;
            E3_ERR Err = EzdKernel.E3_MarkerMarkEnt2(m_idMarker, m_idEM, e3_ID, MarkEntMode.SendDataNotMark,
                0, 1);
            E3_ERR Err1 = EzdKernel.E3_MarkerExecuteAndWaitFinish(m_idMarker, true, 0);
            if (Err1 == E3_ERR.ERR_SUCCESS)
            {
                return Task.FromResult(ret);
            }
            else
            {
                ret = 1;
                return Task.FromResult(ret);
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public Task<bool> MarkerDlgSetCfgAsync()
        {
            bool bReturnOK = false;
            //调用F3参数设置接口,调用后会弹出图2,其中传入要设置的控制卡ID和接收参数改变结果的变量,这里采用的ref传引用的形式.
            //因为接口是阻塞形式调用,在窗口没有关闭的时候,接口不会将控制权返回.
            E3_ERR err = EzdKernel.E3_MarkerDlgSetCfg(m_idMarker, ref bReturnOK);

            return Task.FromResult(bReturnOK);
        }

        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<string> StopMarkAsync()
        {
            EzdKernel.E3_MarkerStop(m_idMarker);

            return Task.FromResult("停止打标");
        }

        public Task<bool> MarkerLaserOn(bool isopen)
        {
            EzdKernel.E3_MarkerSetEntMgr(m_idMarker, m_idEM);
            var a = EzdKernel.E3_MarkerLaserOn(m_idMarker, 1, isopen);
            return Task.FromResult(true);
        }

        public Task<bool> SetPenParm()
        {
            EzdKernel.E3_SetPenParam(m_idEM, 1, new System.Text.StringBuilder("Default"), 0, true, false, 100, 100, 100, 10, 45, 15, 0, 100, 0, 100, 2000, 10, 85, 10, 3, false, 0, 48, false, 1, false, 0, 0, 0, false, 0, 0);

            return Task.FromResult(true);
        }

        public Task<bool> ChangeTextByName(E3_ID e3_ID, string name, string text)
        {
            EzdKernel.E3_ChangeTextByName(e3_ID, name, text);
            return Task.FromResult(true);
        }


        public Task<bool> JumpTo(double x, double y, double z, double speed)
        {
            var aa = EzdKernel.E3_MarkerJumpTo(m_idMarker, x, y, z, speed);
            return Task.FromResult(true);
        }

        public Task<bool> IsAxisHaveHome(int axis, ref bool bIsHaveHome, ref bool bIsAlreadyFindHome)
        {
            var aa = EzdKernel.E3_IsAxisHaveHome(m_idMarker, axis, ref bIsHaveHome, ref bIsAlreadyFindHome);
            return Task.FromResult(true);
        }

        public Task<bool> InitAxis()
        {
            var cc = EzdKernel.E3_InitAllAxis(m_idMarker);
            return Task.FromResult(true);
        }

        public Task<bool> MarkerLaserOff(bool isopen)
        {
            EzdKernel.E3_MarkerLaserOn(m_idMarker, 1, isopen);
            return Task.FromResult(true);
        }

        public Task<bool> MirrorEnt(E3_ID e3_ID, double cenx, double ceny, bool mirrorx, bool mirrory)
        {
            EzdKernel.E3_MirrorEnt(e3_ID, cenx, ceny, mirrorx, mirrory);
            return Task.FromResult(true);
        }

        public Task<E3_ID> Copy()
        {
            var id = EzdKernel.E3_CopyEntity(m_idCurLayer);
            return Task.FromResult(id);
        }

        public Task<bool> Delete(E3_ID e3_ID)
        {
            EzdKernel.E3_DeleteEnt(e3_ID);

            return Task.FromResult(true);
        }
    }
}
